﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using LEEAAttendanceControl.AttendanceReference;

using System.ServiceModel;

using System.Windows.Browser;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.Codes;
using LEEAAttendanceControl.Web.Services;

using System.ServiceModel.DomainServices.Client;
using System.Threading;
using System.Globalization;

namespace LEEAAttendanceControl.Views
{
    public partial class EmployeesView : Page
    {
        LEEAContext context = new LEEAContext();

        public EmployeesView()
        {
            InitializeComponent();
            Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
            Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
            ChangeSummary();
        }

        //--------------------------------------------------------------------
        //--> OnNavigatedTo Event Handler:

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ReloadSpecificSummaryEvent += ReloadSpecificSummaryHandler;

            welcomeText.Text = myApp.DisplayName;

            SummaryBlock.Text = myApp.DisplayName + ": Summary";

            myApp.PeriodFrom = General.General.FirstDayOfCurrentMonth();
            myApp.PeriodTo = DateTime.Now;

            PeriodFrom.Text = myApp.PeriodFrom.ToShortDateString();
            PeriodTo.Text = myApp.PeriodTo.ToShortDateString();
        }

        //--------------------------------------------------------------------
        //--> Reloading Employee Hours Summary Event Handler:

        public void ChangeSummary()
        {
            HoursSummaryDataSource.QueryParameters.Clear();

            Parameter comParameter = new Parameter();

            comParameter.ParameterName = "companyID";
            comParameter.Value = ((App)Application.Current).CompanyID;
            HoursSummaryDataSource.QueryParameters.Add(comParameter);

            Parameter deParameter = new Parameter();

            deParameter.ParameterName = "departmentID";
            deParameter.Value = ((App)Application.Current).DepartmentID;
            HoursSummaryDataSource.QueryParameters.Add(deParameter);

            Parameter emParameter = new Parameter();

            emParameter.ParameterName = "employeeID";
            emParameter.Value = ((App)Application.Current).EmployeeId;
            HoursSummaryDataSource.QueryParameters.Add(emParameter);

            Parameter pfParameter = new Parameter();

            pfParameter.ParameterName = "periodFrom";
            pfParameter.Value = ((App)Application.Current).PeriodFrom.Date;
            HoursSummaryDataSource.QueryParameters.Add(pfParameter);

            Parameter ptParameter = new Parameter();

            ptParameter.ParameterName = "periodTo";
            ptParameter.Value = ((App)Application.Current).PeriodTo.Date;
            HoursSummaryDataSource.QueryParameters.Add(ptParameter);

            HoursSummaryDataSource.Load();
        }

        private void ReloadSpecificSummaryHandler(object sender, EventArgs e)
        {
            ChangeSummary();
        }

        //--------------------------------------------------------------------
        //--> LogOut Event Handler:

        private void hyperLogout_Click(object sender, RoutedEventArgs e)
        {
            InvokeOperation invokeOut = context.LogoutUser(OnLogoutCompleted, null);
        }

        private void OnLogoutCompleted(InvokeOperation result)
        {
            if (result.HasError)
            {
                result.MarkErrorAsHandled();
                MessageDisplay message = new MessageDisplay(ApplicationStrings.GeneralError
                    , ApplicationStrings.GeneralError, "Error");

                message.Show();
            }
            else
            {
                NavigationService.Navigate(new Uri("/Views/Login.xaml", UriKind.Relative));

                App current = (App)Application.Current;

                current.UserName = String.Empty;
                current.UserRole = String.Empty;
                current.DisplayName = String.Empty;

                current.CompanyID = null;
                current.DepartmentID = null;
                current.EmployeeId = 0;

                current.ClearEvents();
            }
        }

        //--------------------------------------------------------------------
        //--> Load Structure Tree:

        private void structureTree_Loaded(object sender, RoutedEventArgs e)
        {
            ReloadTreeHandler(sender, new EventArgs());
        }

        public void ReloadTreeHandler(object sender, EventArgs e)
        {
            AttendanceServiceClient proxy = General.General.GetProxy();

            proxy.BuildEmployeeStructureCompleted += OnBuildEmployeeStructureCompleted;

            proxy.BuildEmployeeStructureAsync(DateTime.Now, ((App)Application.Current).UserName);
        }

        public void OnBuildEmployeeStructureCompleted(object sender, BuildEmployeeStructureCompletedEventArgs e)
        {
            structureTree.ItemsSource = new List<TreeNode>() { e.Result };

            ExpandStructureTree(e.Result);
        }

        //--> Method to expand the whole Structure Tree after loading it:
        public void ExpandStructureTree(TreeNode root)
        {
            structureTree.UpdateLayout();

            TreeViewItem parentNode = (TreeViewItem)structureTree.ItemContainerGenerator.ContainerFromItem(root);

            parentNode.IsExpanded = true;

            foreach (var child in parentNode.Items)
            {
                parentNode.UpdateLayout();

                TreeViewItem childNode = (TreeViewItem)parentNode.ItemContainerGenerator.ContainerFromItem(child);

                childNode.IsExpanded = true;
            }
        }

        //--------------------------------------------------------------------
        //--> LoadingData Event Handler for 'HoursSummaryDataSource':

        private void HoursSummaryDataSource_LoadingData(object sender, LoadingDataEventArgs e)
        {
            e.LoadBehavior = LoadBehavior.RefreshCurrent;
        }

        //--------------------------------------------------------------------
        //--> Apply Accounting Period:

        private void applyPeriodButton_Click(object sender, RoutedEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.PeriodFrom = PeriodFrom.SelectedDate.Value.Date;
            myApp.PeriodTo = PeriodTo.SelectedDate.Value.Date;
        }

        //--------------------------------------------------------------------
        //-- Accordion Events:

        private void taskDion_Loaded(object sender, RoutedEventArgs e)
        {
            EmpHoursItem.IsSelected = true;
        }

        //--------------------------------------------------------------------
        //--> Employee Hours Accordion Item Events:

        private void EmpHoursItem_Selected(object sender, RoutedEventArgs e)
        {
            mainFrame.Navigate(new Uri("/Views/SpecificHours.xaml", UriKind.Relative));
        }

        //--------------------------------------------------------------------
        //--> Reports Accordion Item Events:

        private void ReportsItem_Selected(object sender, RoutedEventArgs e)
        {
            mainFrame.Navigate(new Uri("/Views/SpecificReports.xaml", UriKind.Relative));
        }

        private void SummaryCardRepLink_Click(object sender, RoutedEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ReportName = "Summary Time Card Report";
        }

        private void DetailedCardRepLink_Click(object sender, RoutedEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ReportName = "Detailed Time Card Report";
        }

        private void DetailedOvertimeRepLink_Click(object sender, RoutedEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ReportName = "Detailed Overtime Report";
        }

        private void ShiftAssignmentRepLink_Click(object sender, RoutedEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ReportName = "Shift Assignments Report";
        }

        private void ExAssignmentRepLink_Click(object sender, RoutedEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ReportName = "Exception Assignments Report";
        }

        private void LateComerRepLink_Click(object sender, RoutedEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ReportName = "Late Comers Report";
        }

        private void AbsenceRepLink_Click(object sender, RoutedEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ReportName = "Absence Report";
        }

        private void SavedHoursRepLink_Click(object sender, RoutedEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ReportName = "Saved Hours Report";
        }

        private void MissingPunchRepLink_Click(object sender, RoutedEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ReportName = "Missing Punches Report";
        }

        //--------------------------------------------------------------------
        //--> Structure Details Accordion Item Events:

        private void StructureItem_Selected(object sender, RoutedEventArgs e)
        {
            mainFrame.Navigate(new Uri("/Views/SpecificCompany.xaml", UriKind.Relative));
        }

        //--------------------------------------------------------------------
        //--> Shifts Accordion Item Events:

        private void ShiftsItem_Selected(object sender, RoutedEventArgs e)
        {
            mainFrame.Navigate(new Uri("/Views/SpecificShifts.xaml", UriKind.Relative));
        }

        //--------------------------------------------------------------------
        //--> Exceptions Accordion Item Events:

        private void ExceptionsItem_Selected(object sender, RoutedEventArgs e)
        {
            mainFrame.Navigate(new Uri("/Views/SpecificExceptions.xaml", UriKind.Relative));
        }
    }
}
